package com.lly.tboc.zhouyi.everyday

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.lly.tboc.zhouyi.vo.Gua
import com.lly.tboc.zhouyi.vo.Yao
import com.lly.ui.Dial
import com.lly.ui.DialPlayer
import com.lly.ui.GuaView
import kotlinx.coroutines.launch

private data class TemplateData(
    val guaId: Int = -1,
    val yaoCi: String = "", val yaoList: List<com.lly.ui.Yao> = emptyList()
)

@Composable
fun EverydayOneGuaUI(
    guaList: State<List<Gua>>,
    scrollToGua: (guaId: Int) -> Unit,
    modifier: Modifier = Modifier,
    getYao: suspend (Int, Int) -> Yao?
) {

    val hasData = guaList.value.isNotEmpty()
    AnimatedVisibility(hasData) {
        Column(
            horizontalAlignment = Alignment.CenterHorizontally,
            modifier = Modifier.fillMaxWidth()
        ) {
            var scope = rememberCoroutineScope()
            val data = guaList.value
            var templateData by remember {
                mutableStateOf(TemplateData())
            }
            val dialPlayer = remember(data) {
                DialPlayer(data.map { it.name }, xAxis = 4f) { id ->
                    val guaId = data[id].id
                    scope.launch {
                        val yaoId = EveryDayOneGua.randomYao(guaId)
                        val gua = data[id]
                        val yaoList = Yao.yaoNames(gua.outer, gua.inner)
                            .map { com.lly.ui.Yao(it.isYang()) }.take(6)
                        val yaoCi = getYao(guaId, yaoId)?.ci ?: ""
                        templateData = TemplateData(guaId, yaoCi, yaoList)
                        if (yaoId == 7) {
                            yaoList.forEach { it.isSelected.value = true }
                        } else {
                            yaoList[yaoId - 1].isSelected.value = true
                        }
                    }
                }
            }
            var isBetter by remember {
                mutableStateOf(true)
            }
            Dial(
                dialPlayer, modifier = modifier
                    .size(300.dp, 300.dp)
                    .clickable { isBetter = !isBetter }
                    .padding(10.dp)
            ) {
                EveryDayOneGua.randomGua() - 1
            }
            if (isBetter) {
                Better(templateData, dialPlayer.idle, scrollToGua)
            } else {
                TooBad(templateData, dialPlayer.idle, scrollToGua)
            }
        }
    }
}

@Composable
private fun TooBad(
    data: TemplateData,
    idle: State<Boolean>,
    scrollToGua: (guaId: Int) -> Unit
) {
    val (guaId, yaoCi, yaoList) = data
    Row {
        AnimatedVisibility(visible = yaoList.isNotEmpty() && idle.value) {
            GuaView(
                yaoes = yaoList,
                selectedColor = MaterialTheme.colorScheme.primary,
                color = MaterialTheme.colorScheme.secondary,
                modifier = Modifier
                    .size(30.dp, 60.dp)
                    .padding(8.dp)
            )
        }
        Column {
            if (yaoCi.isNotEmpty() && idle.value) {
                Text(text = yaoCi)
            }
            AnimatedVisibility(visible = guaId > 0 && idle.value) {
                Button(onClick = { scrollToGua(guaId) }) {
                    Text(text = "滚动到该卦")
                }
            }

        }
    }
}

@Composable
private fun Better(
    data: TemplateData,
    idle: State<Boolean>,
    scrollToGua: (guaId: Int) -> Unit
) {
    AnimatedVisibility(visible = idle.value) {
        val (guaId, yaoCi, yaoList) = data
        Row {
            if (yaoList.isNotEmpty()) {
                GuaView(
                    yaoes = yaoList,
                    selectedColor = MaterialTheme.colorScheme.primary,
                    color = MaterialTheme.colorScheme.secondary,
                    modifier = Modifier
                        .size(60.dp, 60.dp)
                        .padding(8.dp)
                )
            }
            Column {
                if (yaoCi.isNotEmpty()) {
                    Text(text = yaoCi)
                }
                AnimatedVisibility(visible = guaId > 0) {
                    Button(onClick = { scrollToGua(guaId) }) {
                        Text(text = "滚动到该卦")
                    }
                }

            }
        }

    }
}

